#ifndef GLOBALS_H
#define GLOBALS_H

//--------------------------------------------------------------------------------------------- Includes
#include "avrlibdefs.h"
#include "avrlibtypes.h"
#include <stddef.h>
#include <inttypes.h>
#include <avr/io.h>

//--------------------------------------------------------------------------------------------- Debugging Flags
//--------------------------------------------------------------------------------------------- Setup the Board speeds and comm speeds here
#define VERSION		0x25
#define PROTOCOL	0x1C		// Start of new Protocol Verion Number
#ifndef SPEED
	//#define SPEED 16
	//#define SPEED 12
	//#define SPEED 8
#endif

//#define USE_UART2

#ifdef USE_UART2
#warning "Using UART2!!!! DO NOT RELEASE THIS"
#endif

#if SPEED == 16
#define F_CPU (16000000)
#define UART0_DEFAULT_BAUD_RATE 500000
#endif

#if SPEED == 12
#define F_CPU (12000000)
#define UART0_DEFAULT_BAUD_RATE 375000
#endif

#if SPEED == 8
#define F_CPU (8000000)
#if HWVER != 31
#define UART0_DEFAULT_BAUD_RATE 250000
#else
#define UART0_DEFAULT_BAUD_RATE 250000
#endif
#endif

//#define HWVER 3
// Hadware definition library
#if HWVER == 2
#include "Hardware_V2.h"
#elif HWVER == 3
#include "Hardware_V3.h"
#elif HWVER == 31
#include "Hardware_V31.h"
#elif HWVER == 4
#include "Hardware_V4.h"
#endif
// Disables the crc check on frames
//#define NO_CRC
//#define DEBUGTIMER
// Enabled complete eeprom read address range
//#define DEBUG_MODE
//#define TEST_MODE

//#define DEBUG
//#define EMUTEST
#ifdef DEBUG
#warning "DEBUG Test Enabled"
	//
#endif

//------------------------------------ ADC Defs
#define ADC_TOP				0xFFC0

//------------------------------------ TICK COUNTS
#define CPU_TICKS			(mGlobalData.mCPUInfo.nTicksMS * TIMERSCHEDULEMS)
#define TICKS				mGlobalData.mCPUInfo.nTicksMS
#define SMALL_TICKS			mGlobalData.mCPUInfo.nTicks
#define TICKS_TIMER			0
#define TICKS_CNT			TCNT0
#define TICKS_CNT_TOP		OCR0A
#define TICKS_PER_SEC		TicksPerSec

//------------------------------------ Timer and Globals
#define CONSULT_BUFFER		0x12
#define TRIGGER_COUNT		32
#define AD_INPUTS_CNT		3
#define	SWITCH_CNT			6
#define SWADC_CNT			8
#define OUTPUT_CNT			5
#define CONSULT_DETAULTS	0x00002903
#define SWADC_TOGGLE		0x100

#define On					0x01;
#define Off					0x00;

#define DQ0		0x01
#define DQ1		0x02
#define DQ2		0x04
#define DQ3		0x08
#define DQ4		0x10
#define DQ5		0x20
#define DQ6		0x40
#define DQ7		0x80

#define FLASH_FAIL			0x00
#define FLASH_OK			0x01
#define FLASH_TIMEOUT		0x02

//--------------------------------------------------------------------------------------------- Default response codes
#define CMD_OK				0x01
#define CMD_FAILED			0x02
#define CMD_CRC_FAILED		0x03
#define CMD_NEXT			0x04
#define SIG_FAILED			0x05
#define BUFFER_FULL			0x06
#define CMD_INCOMPLETE		0x07
#define CMD_UNKNOWN_SENDER	0x08
#define CMD_UNKNOWN_RECIVER	0x09
#define CMD_BAD_HEADS		0x0A
#define CMD_STOP			0x0B
#define CMD_GO				0x0C
#define CMD_PASSED			0x0D
#define CMD_NOTPASSED		0x0F
#define CMD_UNKNOWN			0x10
#define CMD_TOOBIG			0x11
#define CMD_TIMEOUT			0x1E
#define FLASH_WRITE_TIMEOUT 0x1F
#define FLASH_WRITE_FAILED	0x20
#define WATCHDOG_RESET		0x21
#define SEC_BAD_CODE		0x30
#define SEC_FUNCTION_LOCKED 0x31
#define CMD_BOOT			0x60
#define CMD_READY			0xFF


#define ID_CONSULT_PASS			0x91
#define ID_CONSULT_INTEGRATED	0x92
#define ID_FLASH_DATA			0x80
#define ID_EEPROM_DATA			0x82
#define ID_FLASH_COPY			0x84
#define ID_GENERAL_SPECIFIC		0x55
#define ID_LAST_COMMAND_STATUS	0xAA
#define ID_RUN_COMMAND			0xE0
#define ID_BOOTLOADER			0x70
#define ID_TST					0x10
#define ID_ATMEL				0x2A
#define ID_PC					0x1A
#define ID_ECU					0x0A

//--------------------------- Switch Config ID's
#define SCID_UNCONFIGURED	0xFF

//--------- Enable Level 1
#define SCID_CHANGEMAP		0x00

//--------- Enable Level 2
#define SCID_HILOBOOST		0x10

//--------------------------- Bik Output Config ID's
#define BCID_UNCONFIGURED	0xFF

//--------- Enable Level 2
#define BCID_BOOSTCONTROL	0x10
#define BCID_PID			0x11
#define BCID_ALU			0x12

//--------- Enable Level 3
#define BCID_GENERALONOFF	0x21

//--------- Enable Level 4
#define BCID_TURBOTIMER		0x30

//--------- Enable Level 5
#define BCID_GENERALPWM		0x40

//--------- Enable Level 6
#define BCID_DAC			0x50

//--------- Table Types
#define BCTT_TRIMPER		0x00
#define BCTT_TRIMVAL		0x01
#define BCTT_BASE			0x02
#define BCTT_ONOFF			0x03
#define BCTT_MAPCHANGE		0x04
#define BCTT_LOOKUP32		0x20

//--------- Table Variables
#define BCTV_RPM			0x00
#define BCTV_AFM			0x01
#define BCTV_TPS			0x02
#define BCTV_MAP			0x03
#define BCTV_TEMP			0x04
#define BCTV_SPEED			0x05
#define BCTV_BKI1 			0x20
#define BCTV_BKI2 			0x21
#define BCTV_BKI3 			0x22
#define BCTV_BKI4 			0x23
#define BCTV_BKI5 			0x24
#define BCTV_BKAD1 			0x25
#define BCTV_BKAD2 			0x26
#define BCTV_BKAD3 			0x27
#define BCTV_BKAD4			0x28
#define BCTV_BKAD5			0x29
#define BCTV_BKAX1 			0x2A
#define BCTV_BKAX2 			0x2B
#define BCTV_BKAX3 			0x2C
#define BCTV_BKAX4 			0x2D
#define BCTV_BKAX5 			0x2E
#define BCTV_TRIG1 			0x30
//      BCTV_TRIG2-19 		0x31 - 0x42
#define BCTV_TRIG20 		0x43
#define BCTV_TRIG32			0x50
#define BCTV_BKADLU1 		0x60
#define BCTV_BKADLU2 		0x61
#define BCTV_BKADLU3 		0x62
#define BCTV_BKI6			0x6A
#define BCTV_BKI7			0x6B
#define BCTV_BKI8			0x6C
#define BCTV_BKAD6			0x6D
#define BCTV_BKAD7			0x6E
#define BCTV_BKAD8			0x6F
#define BCTV_AL1			0x70
//		BCTV_AL2-63 0x71 - 0xAF
#define	BCTV_AL64			0xAF

//--------- Trigger Types
#define BCTT_HEX			0x00
#define BCTT_RANGE			0x01

//--------- Comparitors
#define GTE 	0x00
#define LTE 	0x01
#define EQ 		0x02
#define GT 		0x03
#define LT 		0x04
#define NE 		0x05

#define AND 	0x00
#define NAND 	0x01
#define OR 		0x02
#define NOR 	0x03
#define XOR 	0x04
#define NXOR 	0x05

#define RUN_ERASEALL 			0x80
#define RUN_ERASEALL_BANK1		0x81
#define RUN_ERASEALL_BANK2		0x82
#define RUN_ERASESECTOR 		0x88
#define RUN_SEC_UNLOCKCODE 		0x90
#define RUN_SEC_SETPASSWORD 	0x92
#define RUN_SEC_SETSECLEVEL 	0x94
#define RUN_GET_INFO 			0x70
#define RUN_DIAG_RXTX			0x60
#define RUN_DIAG_RXCLK			0x61
#define RUN_DIAG_FLASH			0x62
#define RUN_DIAG_FLASH2			0x63
#define RUN_WRITE_PATTERN		0x64
#define RUN_GET_BOARD_ADC		0x67
#define RUN_TEST_OUTPUTS		0x6A
#define RUN_SET_TRACE_ADR		0x6B

#define HEADERS_SIZE				10
#define CONSULT_INITIALISE_REQUIRED 0x10

#define WRITE			0x55
#define READ			0xAA

#define CYCLES_PER_US ((F_CPU+500000)/1000000) 
#define OVERFLOW_PER_SECOND ((F_CPU/(8*65536)))

//------------------------------------------------------------------------------------------------ Default Communication Settings
//! Default uart baud rate.
/// This is the default speed after a uartInit() command,
/// and can be changed by using uartSetBaudRate().
//#define UART0_DEFAULT_BAUD_RATE		9600	///< default baud rate for UART0
#define UART1_DEFAULT_BAUD_RATE		9600	///< default baud rate for UART1

// buffer memory allocation defines
// buffer sizes
#ifndef UART0_TX_BUFFER_SIZE
#define UART0_TX_BUFFER_SIZE		0x0100	///< number of bytes for uart0 transmit buffer
#endif
#ifndef UART0_RX_BUFFER_SIZE
#define UART0_RX_BUFFER_SIZE		0x0420	///< number of bytes for uart0 receive buffer
#endif
#ifndef UART1_TX_BUFFER_SIZE
#define UART1_TX_BUFFER_SIZE		0x0020	///< number of bytes for uart1 transmit buffer
#endif
#ifndef UART1_RX_BUFFER_SIZE
#define UART1_RX_BUFFER_SIZE		0x0040	///< number of bytes for uart1 receive buffer
#endif

//--------------------------------------------- ALU Units stuff

#define MULTIPLY			0x00
#define ADDITION			0x01
#define POWEROF				0x02
#define DIVIDE				0x03
#define SUBTRACT			0x04
#define COS					0x05
#define ACOS				0x06
#define SIN					0x07
#define ASIN				0x08
#define TAN					0x09
#define ATAN				0x0A
#define LOG					0x0B
#define ALU_NOCONFIG		0x3F

#define ALU_UNITS			64
//#define ALU_CALCS			32
#define ALU_BYTES_PER_ALU	9
#define ALU_BYTES_PER_CALC	2

//------------------------------------------------------------------------------------------------- EEprom Important Locations
#define EE_DAC_OE			0x0960
#define EE_SD_DTS			0x0961
//#define EE_ALU_CALC		0x0E00
#define EE_ALU_UNIT			0x0D10
#define EE_ALU_LU			0x0F70

#define EE_BK1 				0x0300
#define EE_BK2 				0x0400
#define EE_BK3 				0x0500
#define EE_BK4 				0x0600
#define EE_BKT1 			0x0700
#define EE_BKT2 			0x0710
#define EE_BKT3 			0x0720
#define EE_BKT4 			0x0730
#define EE_BKT5 			0x0740
#define EE_BKT6 			0x0750
#define EE_BKT7 			0x0760
#define EE_BKT8 			0x0770
#define EE_BKT9 			0x0780
#define EE_BKT10 			0x0790
#define EE_BKT11 			0x07A0
#define EE_BKT12 			0x07B0
#define EE_BKT13 			0x07C0
#define EE_BKT14 			0x07D0
#define EE_BKT15 			0x07F0
#define EE_BKT16 			0x0800
#define EE_BKT17 			0x0810
#define EE_BKT18 			0x0820
#define EE_BKT19 			0x0830
#define EE_BKT20 			0x0840
#define EE_BKT21 			0x0850
#define EE_BKT22 			0x0860
#define EE_BKT23 			0x0870
#define EE_BKT24 			0x0880
#define EE_BKT25 			0x0890
#define EE_BKT26 			0x08A0
#define EE_BKT27 			0x08B0
#define EE_BKT28 			0x08C0
#define EE_BKT29 			0x08D0
#define EE_BKT30 			0x08E0
#define EE_BKT31 			0x08F0
#define EE_BKT32 			0x0850

#define EE_LOOKUP_A 0x0990
#define EE_LOOKUP_B 0x0AA0
#define EE_LOOKUP_C 0x0BB0

#define getTrigger(x) EE_BKT ## x

#define EE_SECCODE_0 0x0010
#define EE_SECCODE_1 0x0020
#define EE_SECCODE_2 0x0030
#define EE_SECCODE_3 0x0040
#define EE_SECCODE_4 0x0050
#define EE_SECCODE_5 0x0060
#define EE_SECCODE_6 0x0070
#define EE_SECCODE_7 0x0080
#define EE_USER0 	0x0090
#define EE_USER1	0x00A0
#define EE_USER2	0x00B0

#define EE_SECURITYBYTE 0x0001
#define EE_USERBYTE 0x0000
#define EE_BOARD_SERIAL_NO 0x000C

#define EE_CONSULT_BITSET 0x097E

#define EE_START 0x0300
#define EE_END	0x0CFF

#define SEC_LV1 0x07
#define SEC_LV2 0x06
#define SEC_LV3 0x05
#define SEC_LV4 0x04
#define SEC_LV5 0x03
#define SEC_LV6 0x02
#define SEC_LV7 0x01
#define SEC_LV8 0x00

//----------------------- Macro's
#define ccbi(VAR) cbi(VAR)
#define ssbi(VAR) sbi(VAR)
#define pulseHi(_BIK_PORT) ssbi(PORT_ ## _BIK_PORT); ccbi(PORT_ ## _BIK_PORT)
#define pulseLo(_BIK_PORT) ccbi(PORT_ ## _BIK_PORT); ssbi(PORT_ ## _BIK_PORT)

//----------------------- Consult Bits
#define CB_RPM_HI 			0x00
#define CB_RPM_LO 			0x01
#define CB_AFM_HI 			0x04
#define CB_AFM_LO 			0x05
#define CB_TEMP 			0x08
#define CB_LH_O2 			0x09
#define CB_RH_O2 			0x0A
#define CB_SPEED 			0x0B
#define CB_BVOLT 			0x0C
#define CB_TPS 				0x0D
#define CB_DBR1 			0x13
#define CB_INJ_HI 			0x14
#define CB_INJ_LO 			0x15
#define CB_IGN 				0x16
#define CB_AAC 				0x17
#define CB_LOAD 			0x0F

#endif
